<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">机构管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a><cite>系统管理</cite></a>
          <a><cite>机构管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <!-- 搜索条件 -->
        <div class="layui-form toolbar">
            所属公司：<select id="company" lay-search>></select>&nbsp;&nbsp;&nbsp;
            <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="operate"><i
                    class="layui-icon">&#xe63c;</i>操作日志
            </button>
        </div>
        <div id="orgChart"></div>
    </div>
</div>
<link href="/assets/libs/orgChart/org.css" media="all" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/assets/libs/orgChart/org.js"></script>
<script type="text/javascript">
    function loadOrg(crab, companyId) {
        crab.get('/sys/org/list_' + companyId, {}, function (data) {
            var orgChart = $('#orgChart').orgChart({
                data: data,
                showControls: true,
                allowEdit: true,
                newNodeText: '添加子部门',
                onAddNode: function (node) {
                    //log('Created new node on node ' + node.data.id);
                    //org.addNode({id: 2, name: '111', pid: 1});
                    orgChart.newNode(node.data.id);
                },
                onDeleteNode: function (node) {
                    log('Deleted node ' + node.data.id);
                    if (node.data.pid == 0) {
                        console.log(1)
                    } else {
                        orgChart.deleteNode(node.data.id);
                    }
                },
                onClickNode: function (node) {
                    console.log('--sd---', node.data)
                    console.log('---data---', orgChart.getData());
                }
            });
        });
    }

    layui.use(['form', 'table', 'config', 'crab'], function () {
        var form = layui.form;
        var crab = layui.crab;
        crab.get('/sys/company/list_effective', {}, function (data) {
            var l = data.length;
            if (l > 0) {
                var option = '';
                for (var i = 0; i < l; i++) {
                    option += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                }
                $('#company').append(option);
                form.render('select');
                // 加载机构
                loadOrg(crab, data[0].id);
            }
        });
    });
</script>
